{
*******************************************************************************
-------------------------------------------------------------------------------
Kliper - next generation multimedia player
Copyright (C) Marcin 'vi4m' Kliks (vi4m@wp.pl)
Copyright (C) Tomasz 'dymsza' Odrobny (dymsza@gmail.com)
-------------------------------------------------------------------------------
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*******************************************************************************
}

unit update;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, regexpr, os, ExtCtrls, utils, defaults, shellApi,
  HTTPGet;

type
  Tup2date = class(TForm)
    Button1: TButton;
    GroupBox1: TGroupBox;
    log: TMemo;
    progress: TProgressBar;
    http: THTTPGet;
    Label1: TLabel;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure execute;
    procedure httpError(Sender: TObject);
    procedure httpProgress(Sender: TObject; TotalSize, Readed: Integer);
    procedure httpDoneFile(Sender: TObject; FileName: string;
      FileSize: Integer);
    procedure httpDoneString(Sender: TObject; Result: string);
    procedure Button1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    note: string;
    aborted: integer;
    labstr: string;
    speed: integer;
    downloaded: int64;
    last_downloaded: int64;


  public

  end;




var
  up2date: Tup2date;
  s: tfilestream;
  pos: int64 = 0;
  file_size: int64 = 0;
  filep: string;
  parameters: string;
  started: boolean;

  prog: integer;


implementation
uses main, StrUtils;

{$R *.dfm}

procedure Tup2date.FormCreate(Sender: TObject);
begin
  SetWindowPos(
    handle,
    HWND_TOP,
    0,
    0,
    0,
    0,
    SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
  parameters := extractfiledir(paramstr(0));
  speed := 0;
  aborted := 0;
  started := false;


end;

procedure tup2date.Execute;
var
  stat: string;
  filepath: string;
  x: tregexpr;
  list: tstringlist;
  filesize: string;
  changes: string;
  downstring: string;

begin

  x := tregexpr.Create;
  list := tstringlist.Create;
  try
    http.BinaryData := false;
    downstring := download_url; // + '?my_version=' + kliper_version+'?my_os='+inttostr(os.GetOperatingSystem);
    http.WaitThread := true;
    http.URL := downstring;
    log.lines.Add(translatestr('KTS_INFO_UP2DATE_CHECKING_FOR_UPDATES'));
    http.Getstring;
    stat := note;
    http.WaitThread := false;
    http.BinaryData := true;


    x.Expression := '#';
    x.Split(stat, list);
    if list.Count = 3 then begin
      filepath := list[0];
      filesize := list[1];
      changes := list[2];
      if filepath = '' then begin
        aborted := 2;
        showinfo('KTS_INFO_UP2DATE_NONEWUPDATES');
        close;
        exit;
      end;
      if MessageDlg(translatestr('KTS_INFO_UP2DATE_ASKDOWNLOADANDRUN', changes,
        filesize), mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
        http.URL := filepath;
        http.FileName := GetEnvironmentVariable('TEMP') + '\kliper-setup.exe';
        log.lines.Add(translatestr('KTS_INFO_UP2DATE_DOWNLOADING_UPDATES'));
        http.GetFile;
      end else begin
        log.Lines.Clear;
        started := false;
        button1.Enabled := true;
      end;
    end;

  finally
    x.Free;
    list.Free;
  end;
end;



procedure Tup2date.httpError(Sender: TObject);
begin
  if aborted = 1 then
    showerror('KTS_ERROR_UP2DATE_DOWNLOADING_ABORTED') else
    showerror('KTS_ERROR_UP2DATE_DOWNLOADING_FILE');
  aborted := 2;

end;

procedure Tup2date.httpProgress(Sender: TObject; TotalSize,
  Readed: Integer);
begin
  if totalsize <> 0 then begin

    label1.Visible := true;
    label1.Caption := filter(labstr, inttostr(readed div 1024), inttostr(speed), inttostr((totalsize - readed) div 1024));
    progress.Position := readed * 100 div totalsize;
    downloaded := readed;
  end;

end;

procedure Tup2date.httpDoneFile(Sender: TObject; FileName: string;
  FileSize: Integer);

begin
  log.lines.Add(translatestr('KTS_INFO_UP2DATE_DOWNLOADED_TO') + ' ' + FileName + ' Size: ' + inttostr(FileSize));
  progress.Position := 0;
  parameters := ' /dir="' + parameters + '" /SP- /SILENT /LOG="' + parameters + '\kliper-setup-log.txt"';
  shellexecute(handle, 'open', pchar(filename), pchar(parameters), pchar(extractfiledir(paramstr(0))), SW_SHOW);
  application.Terminate;
end;

procedure Tup2date.httpDoneString(Sender: TObject; Result: string);
begin
  note := result;
end;

procedure Tup2date.Button1Click(Sender: TObject);
begin
  button1.Enabled := false;
  started := true;
  execute;
end;


procedure Tup2date.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if started then begin
    if aborted <> 2 then aborted := 1;
    http.Abort;
    application.ProcessMessages;
    http.WaitThread := true;
    while aborted <> 2 do begin
      application.processmessages;
    end;
  end;
  canclose := true;
end;

procedure Tup2date.FormShow(Sender: TObject);
begin
  labstr := label1.Caption;
end;

procedure Tup2date.Timer1Timer(Sender: TObject);
begin
  speed := (downloaded - last_downloaded) div 2048;
  last_downloaded := downloaded;
end;

end.
